﻿'******************************* File Header *********************************
'* File Name:    WmiDataReaderTest.vb
'* Project:      WmiConnection.Test
'* Copyright (c) Freddy Kuehne
'*
'* Unit tests for WmiDataReader.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************
Imports System.Text
Imports System.Data.Common

<TestClass()>
Public Class WmiDataReaderTest
#Region "Tests"
  <TestMethod()>
  Public Sub HierarchyTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using rd As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(GetType(WmiDataReader).IsInstanceOfType(rd))
          rd.Close()
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CloseTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsFalse(target.IsClosed)

          ' no exception
          target.Close()
          Assert.IsTrue(target.IsClosed)
          target.Close()
          Assert.IsTrue(target.IsClosed)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub DepthTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.AreEqual(0, target.Depth)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub FieldCountTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.AreEqual(1, target.FieldCount)
        End Using
      End Using

      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version, Description FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.AreEqual(2, target.FieldCount)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetBooleanTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsFalse(target.GetBoolean(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetByteTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CByte(0), target.GetByte(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(NotSupportedException))>
  Public Sub GetBytesTest()
    Dim buffer(256) As Byte
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CByte(0), target.GetBytes(0, 0, buffer, 0, buffer.Length))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetCharTest()
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version.Chars(0), target.GetChar(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(NotSupportedException))>
  Public Sub GetCharsTest()
    Dim buffer(256) As Char
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version.Length, target.GetChars(0, 0, buffer, 0, buffer.Length))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetDataTypeNameTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(GetType(String).FullName, target.GetDataTypeName(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetDateTimeTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT LastBootUpTime FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsTrue(Now.AddMonths(-1).CompareTo(target.GetDateTime(0)) < 0)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetDecimalTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CDec(0), target.GetDecimal(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetDoubleTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CDbl(0), target.GetDouble(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetFieldTypeTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(GetType(Boolean), target.GetFieldType(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetFloatTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CSng(0), target.GetFloat(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetGuidTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT SettingID FROM Win32_NetworkAdapterConfiguration"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsTrue(GetType(Guid).IsInstanceOfType(target.GetGuid(0)))
          Assert.AreNotEqual(Guid.Empty, target.GetGuid(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetInt16Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CShort(0), target.GetInt16(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetInt32Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CInt(0), target.GetInt32(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetInt64Test()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(CLng(0), target.GetInt64(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetNameTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual("Debug", target.GetName(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetOrdinalTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Debug FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(0, target.GetOrdinal("Debug"))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetStringTest()
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version, target.GetString(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetValueTest()
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version, target.GetValue(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub GetValuesTest()
    Dim values(1) As Object
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          target.GetValues(values)
          Assert.AreEqual(version, values(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(ArgumentNullException))>
  Public Sub GetValuesExceptionTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          target.GetValues(Nothing)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub IsClosedTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsFalse(target.IsClosed)
          target.Close()
          Assert.IsTrue(target.IsClosed)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub IsDBNullTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Caption, GUID FROM Win32_NetworkAdapter WHERE GUID IS NULL"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsFalse(target.IsDBNull(0))
          Assert.IsTrue(target.IsDBNull(1))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub Item1Test()
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version, target.Item(0))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub Item2Test()
    Dim version As String
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      version = con.ServerVersion
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.AreEqual(version, target.Item("Version"))
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub NextResultTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsFalse(target.NextResult)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ReadTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsFalse(target.Read)
          Assert.IsFalse(target.Read)
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub RecordsAffectedTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.AreEqual(1, target.RecordsAffected)
          Assert.IsTrue(target.Read)
          Assert.AreEqual(1, target.RecordsAffected)
        End Using
      End Using

      Using cmd As DbCommand = con.CreateCommand
        cmd.CommandText = "SELECT Caption FROM CIM_Printer"
        Using target As IDataReader = cmd.ExecuteReader
          Assert.IsTrue(target.Read)
          Assert.IsTrue(target.RecordsAffected > 1)
        End Using
      End Using
    End Using
  End Sub
#End Region
End Class
